package com.example.watermark;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.omg.PortableInterceptor.INACTIVE;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WindowJoinDemo
 * Package: com.example.watermark
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-20
 * Time: 16:06
 */

//基于时间的合流
public class WindowJoinDemo {
    public static void main(String[] args) throws Exception {

        //创建执行环境
        StreamExecutionEnvironment env =
                StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行度
        env.setParallelism(1);
        //创建数据源

        SingleOutputStreamOperator<Tuple2<String, Integer>> ds1 = env.fromElements(
                Tuple2.of("a", 1),
                Tuple2.of("b", 2),
                Tuple2.of("c", 3),
                Tuple2.of("d", 4)
        )
                //指定水位线
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<Tuple2<String, Integer>>forMonotonousTimestamps() //升序
                                .withTimestampAssigner((val, ts) -> val.f1 * 1000L)
                );

        SingleOutputStreamOperator<Tuple3<String, Integer,Integer>> ds2 = env.fromElements(
                Tuple3.of("a", 1,1),
                Tuple3.of("b", 2,2),
                Tuple3.of("c", 12,1),
                Tuple3.of("d", 21,1)
        )
                //指定水位线
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<Tuple3<String, Integer,Integer>>forMonotonousTimestamps() //升序
                                .withTimestampAssigner((val, ts) -> val.f1 * 1000L)
                );

        //window Join
        //1.落在同一时间窗口范围才能匹配
        //2.根据keyby的key 来进行关联
        //3.只能拿到匹配上数据 类似固定时间范围的inner join
        DataStream<String> apply = ds1.join(ds2)
                .where(r1 -> r1.f0) //ds1的数据
                .equalTo(r2 -> r2.f0)//ds2的数据
                .window(TumblingEventTimeWindows.of(Time.seconds(5))) //开窗方式
                .apply(new JoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>() {
                    /**
                     * 关联的数据
                     * @param first
                     * @param second
                     * @return
                     * @throws Exception
                     */
                    @Override
                    public String join(Tuple2<String, Integer> first, Tuple3<String, Integer, Integer> second) throws Exception {

                        return first + "<------------->" + second;

                    }
                });


        apply.print();

        env.execute();

    }
}
